//-----------------------------------------------------------
//
//-----------------------------------------------------------
class ZF_PTRDBullet extends PTRDBullet;

simulated function ProcessTouch(Actor Other, Vector HitLocation)
{
	local Vector	X, Y, Z;
	local float	V;
	local bool	bHitWhipAttachment;
	local ROPawn HitPawn;
	local ROVehicle HitVehicle;
	local ROVehicleWeapon HitVehicleWeapon;
	local bool bHitVehicleDriver;
	local Vector TempHitLocation, HitNormal;
	local array<int>	HitPoints;

	if (Other == Instigator || Other.Base == Instigator || !Other.bBlockHitPointTraces )
		return;

	HitCount++;

	HitVehicleWeapon = ROVehicleWeapon(Other);
	HitVehicle = ROVehicle(Other.Base);

    if( HitVehicleWeapon != none && HitVehicle != none )
    {
		if ( HitVehicleWeapon.HitDriverArea(HitLocation, Velocity) )
		{
			if( HitVehicleWeapon.HitDriver(HitLocation, Velocity) )
			{
				bHitVehicleDriver = true;
			}
			else
			{
				return;
			}
		}

	    if( HitVehicle.IsA('ROTreadCraft') && !ROTreadCraft(HitVehicle).ShouldPenetrate(HitLocation, Normal(Velocity), GetPenetrationNumber(LaunchLocation-HitLocation)))
	    {
			// Spawn the bullet hit effect client side
			if (ImpactEffect != None && (Level.NetMode != NM_DedicatedServer))
			{
		        PlaySound(VehicleDeflectSound,,5.5*TransientSoundVolume,,,1.5);
				Spawn(class'TankAPHitDeflect',,, Location + HitNormal*16,rotator(HitNormal));
			}
	        return;
	    }

		if (Role == ROLE_Authority)
		{
			if ( bHitVehicleDriver )
			{
				HitVehicleWeapon.TakeDamage(Damage - 20 * (1 - VSize(Velocity) / default.Speed), instigator, HitLocation, MomentumTransfer * Normal(Velocity), MyDamageType);
			}
			else
			{
				HitVehicle.TakeDamage(Damage - 20 * (1 - VSize(Velocity) / default.Speed), instigator, HitLocation, MomentumTransfer * Normal(Velocity), MyDamageType);
			}

			MakeNoise(1.0);
		}

	 	if ( Level.NetMode != NM_DedicatedServer)
		{
		    PlaySound(VehicleHitSound,,5.5*TransientSoundVolume,,,1.5);
		    if ( EffectIsRelevant(Location,false) )
		    {
				Spawn(ShellHitVehicleEffectClass,,,HitLocation,rotator(Normal(-Velocity)));
		    }
		}

        // Give the bullet a little time to play the hit effect client side before destroying the bullet
		if (Level.NetMode == NM_DedicatedServer)
		{
			bCollided = true;
			SetCollision(False,False);
		}
		else
		{
			Destroy();
		}

        return;
    }

	V = VSize(Velocity);

	// if the bullet collides right after launch, it doesn't have any velocity yet.
	// Use the rotation instead and give it the default speed - Ramm
	if( V < 25 )
	{
		GetAxes(Rotation, X, Y, Z);
		V=default.Speed;
	}
	else
	{
	  	GetAxes(Rotator(Velocity), X, Y, Z);
	}

 	if( ROBulletWhipAttachment(Other) != none )
	{
    	bHitWhipAttachment=true;

        if(!Other.Base.bDeleteMe)
        {
	        Other = Instigator.HitPointTrace(TempHitLocation, HitNormal, HitLocation + (65535 * X), HitPoints, HitLocation,, 1);

			if( Other == none )
				return;

			HitPawn = ROPawn(Other);
		}
		else
		{
			return;
		}
	}

	if (V > MinPenetrateVelocity * ScaleFactor)
	{
        if (Role == ROLE_Authority)
        {
	    	if ( HitPawn != none )
	    	{
                 if(!HitPawn.bDeleteMe)
				 	HitPawn.ProcessLocationalDamage(Damage - 20 * (1 - V / default.Speed), Instigator, TempHitLocation, MomentumTransfer * X, MyDamageType,HitPoints);
                 bHitWhipAttachment = false;
	    	}
	    	else
	    	{
				Other.TakeDamage(Damage - 20 * (1 - V / default.Speed), Instigator, HitLocation, MomentumTransfer * X, MyDamageType);
			}
		}
		else
		{

	    	if ( HitPawn != none )
	    	{
		        bHitWhipAttachment = false;
	    	}
        }
	}
}

DefaultProperties
{

}
